require('eventHandler')
require('Glow')

menu = {}

menu.font_title = love.graphics.newFont("Fonts/Orbitron Black.ttf", 48)
menu.font_desc = love.graphics.newFont("Fonts/Orbitron Black.ttf", 22)
menu.font_menu = love.graphics.newFont("Fonts/League_Gothic-webfont.ttf", 26)
menu.option = 0
menu.state = 1
menu.run = false

function menu:load(views)
     menu.view = views
     menu.offset = love.graphics.getHeight()/2-(menu.font_menu:getHeight()*#menu.view[menu.state])/2
     
     size_font = 12
     orig_font = love.graphics.newFont(size_font)
     
     orig_r, orig_g, orig_b, orig_a = love.graphics.getColor()
     
     menu.glow = Glow.load(love.graphics.getWidth()/2, love.graphics.getHeight()/2)
end

function menu:toggle()
     menu.run = not menu.run
end

menu.title_fade = 0
function menu:draw()
     if menu.run then
          self.glow:draw()
          
          love.graphics.setColor(255, 255, 255, 96+96*math.abs(math.sin(menu.title_fade)))
          love.graphics.setFont(menu.font_title)
          love.graphics.printf(
               menu.view[menu.state].title,
               love.graphics.getWidth()*0.05,
               love.graphics.getHeight()*0.9 - menu.font_title:getHeight()*2,
               love.graphics.getWidth()*0.6,
               "left"
          )
          if menu.view[menu.state].desc ~= nil then
               love.graphics.setColor(255,255,255,191)
               love.graphics.setFont(menu.font_desc)
               love.graphics.printf(
                    menu.view[menu.state].desc,
                    love.graphics.getWidth()*0.75/10,
                    love.graphics.getHeight()*1/10,
                    love.graphics.getWidth()*5/10,
                    "left"
               )
          end
          love.graphics.setColor(255, 255, 255, 127)
          love.graphics.setFont(menu.font_menu)
          for i, v in ipairs(menu.view[menu.state]) do
               love.graphics.printf(
                    v.t,
                    love.graphics.getWidth()*0.65,
                    (i - 1)*menu.font_menu:getHeight() + menu.offset,
                    love.graphics.getWidth()*0.2,
                    "right"
               )
          end
          love.graphics.setColor(255, 255, 255, 96+96*math.abs(math.sin(menu.title_fade)))
          love.graphics.printf(
               menu.view[menu.state][menu.option+1].t,
               love.graphics.getWidth()*0.65,
               (menu.option)*menu.font_menu:getHeight() + menu.offset,
               love.graphics.getWidth()*0.2,
               "right"
          )
          
          love.graphics.setColor(0, 0, 0, 0)
     end
end

function menu:update(dt)
     if menu.run then
          menu.title_fade = menu.title_fade + dt*0.5
     end
     --Don't call glow:update(), or the glow will shrink
     self.glow.particleSys:update(dt)
     EventHandler:parseInput()
end

function menu:callback()
     cb = menu.view[menu.state][menu.option+1].cb
     if cb == "newGame" then
          -- Reset scheme for drawing
          love.graphics.setFont(orig_font)
          menu:toggle()
     elseif cb == "cred" then
          menu:setState(2)
     elseif cb == "back" then
          menu:setState(1)
     elseif cb == "exit" then
          love.event.quit()
     end
end

function menu:setState(stateID)
     if menu.view[stateID] then
          menu.option = 0
          menu.state = stateID
     else
          print("Menu state does not exist")
     end
end